#include "management.h"

void Print_welcome1(struct clmsg *clmsg){
    printf("---------------------------------------------------------------\n");
    printf("|                                                             |\n");
    printf("|   1.管理员注册 2.员工注册 3.管理员登录 4.员工登录 0.退出    |\n");
    printf("|                                                             |\n");
    printf("---------------------------------------------------------------\n");
    printf("请输入操作码>>");
    clmsg->code=Inputret();
}
void Print_welcome2(struct clmsg *clmsg){
    printf("----------------------------------------------------------\n");
    printf("|                                                        |\n");
    printf("|   5.员工查询 6.员工修改 7.员工添加 8.员工删除 0.退出   |\n");
    printf("|                                                        |\n");
    printf("----------------------------------------------------------\n");
    printf("请输入操作码>>");
    clmsg->code=Inputret();
}

void Print_welcome3(struct clmsg *clmsg){
    printf("----------------------------------------------------------\n");
    printf("|                                                        |\n");
    printf("|              5.员工查询 6.员工修改 0.退出              |\n");
    printf("|                                                        |\n");
    printf("----------------------------------------------------------\n");
    printf("请输入操作码>>");
    clmsg->code=Inputret();
}
//获取操作码
int Inputret(){
    static int num;
    fflush(stdin);
    scanf("%d",&num);
    return num;
}
//管理员注册
void Administer_reguster(int sockfd,struct clmsg*clmsg){
    struct semsg semsg;
    
    memset(clmsg->us_name,0,32);
    printf("请输入用户名>>");
    scanf("%s",clmsg->us_name);
    getchar();

    memset(clmsg->us_pswd,0,128);
    printf("请输入密码>>");
    scanf("%s",clmsg->us_pswd);
    getchar();

    //发送登陆请求
    send(sockfd,clmsg,sizeof(struct clmsg), 0);
    //接受服务器响应
    //ssize_t retnum;
    recv(sockfd,&semsg,sizeof(struct semsg), 0);
     

    // printf("测试注册返回码————————>%d\n",semsg.code);
    // printf("测试注册账号————————>%s\n",clmsg->us_name);
    // printf("测试注册密码————————>%s\n",clmsg->us_pswd);
    if(semsg.code==1){
    printf("注册成功!\n");
    }else if(semsg.code==2){
    printf("用户已存在\n");
    }else{
    printf("注册失败\n");
    }
}

//管理员登录
int Administer_login(int sockfd,struct clmsg*clmsg){
    struct semsg semsg;
    memset(clmsg->us_name,0,32);
    printf("请输入用户名>>");
    scanf("%s",clmsg->us_name);
    getchar();

    memset(clmsg->us_pswd,0,128);
    printf("请输入密码>>");
    scanf("%s",clmsg->us_pswd);
    getchar();

    //发送登陆请求
    send(sockfd,clmsg, sizeof(struct clmsg), 0);
    //接受服务器响应
    recv(sockfd,&semsg,sizeof(struct semsg), 0);

    //判断是否登陆成功
    if(semsg.code==1){
        printf("亲爱的管理员，欢迎您登陆员工管理系统！\n");
        return 0;
    }
    else{
        printf("登陆失败！\n");
        return -1;
    }
}
//管理员添加
void Men_install(int sockfd,struct clmsg *clmsg){
    char temp;
    struct semsg semsg;
    while(1){
        printf("请输入工号>>");
        scanf("%d",&clmsg->member.number);
        getchar();
        printf("请输入用户名>>");
        scanf("%s",clmsg->member.name);
        getchar();
        printf("您输入的姓名是：%s\n",clmsg->member.name);
        printf("姓名信息一旦录入无法更改，请确认您所输入的是否正确！(Y/N)");
        scanf("%c",&temp);
        getchar();
        if(temp == 'N' || temp == 'n'){
            printf("请重新添加用户>>");
            break;
        }

       // printf("请输入用户密码>>");
       // scanf("%s",clmsg->pswd);
       // getchar();

        printf("请输入性别>>");
        scanf("%s",&clmsg->member.sex);
        getchar();

        printf("请输入年龄>>");
        scanf("%d",&clmsg->member.old);
        getchar();

        printf("请输工资>>");
        scanf("%d",&clmsg->member.salary);
        getchar();

        //发送用户数据
        send(sockfd, clmsg, sizeof(struct clmsg), 0);
        //等待接收服务器响应
        recv(sockfd, &semsg, sizeof(struct semsg), 0);

        if(semsg.code==1)
            printf("添加成功！\n");
        else if(semsg.code==0)
            printf("添加失败！\n");
        else
            printf("用户存在\n");
    }
}
//修改
void Mem_change(int sockfd,struct clmsg*clmsg){
    int n;
    struct semsg semsg;
	printf("请输入您要修改只认的用户名>>");
	scanf("%s", clmsg->member.name);
	getchar();
	
    printf("----------------------------------------------------------\n");
    printf("|                                                        |\n");
    printf("|              1.工号 2.性别 3.年龄 4.工资               |\n");
    printf("|                                                        |\n");
    printf("----------------------------------------------------------\n");
    printf("请输入操作码>>");
	scanf("%d",&n);
			    if(semsg.code!=3)
	getchar();
	
	switch(n){
		case 1:
			printf("请输入工号>>");
			scanf("%d",&clmsg->member.number);
            getchar();
			break;
		case 2:
			printf("请输入性别>>");
			scanf("%s",&clmsg->member.sex);
            getchar();
			break;
		case 3:
			printf("请输入年龄>>");
			scanf("%d",&clmsg->member.old);
            getchar();
			break;
		case 4:
			printf("请输入工资：");
			scanf("%d",&clmsg->member.salary);
            getchar();
			break;
	}
	
	send(sockfd,clmsg, sizeof(struct clmsg), 0);
	recv(sockfd,&semsg, sizeof(struct semsg), 0);

	printf("修改结束.\n");

}
//删除
void Mem_edelete(int sockfd,struct clmsg*clmsg){
    struct semsg semsg;
	printf("请输入要删除的工号>>"); //工号唯一
	scanf("%d",&clmsg->member.number);
	getchar();

	send(sockfd, clmsg, sizeof(struct clmsg), 0);
	recv(sockfd, &semsg, sizeof(struct semsg), 0);
	
	if(semsg.code ==1)
		printf("删除成功！\n");
}
//查询
void Mem_dataShow(int sockfd,struct clmsg*clmsg){
    int n,flags;
    struct semsg semsg;
            recv(sockfd,&semsg,sizeof(struct semsg),0);
             printf("收到--%-11s\n",semsg.msg);
                if(semsg.code==1){
                while (semsg.code!=3){
                recv(sockfd, &semsg, sizeof(struct semsg), 0);
                printf("%-11s\n",semsg.msg);
                }
            }else{
            printf("查询失败\n");
            }			
}
//服务器相应管理员注册
void server_add(int sockfd,struct semsg*semsg,struct clmsg clmsg){
    ssize_t retnum;
    semsg->code = Sq_install_AD(clmsg);
    retnum=send(sockfd,semsg,sizeof(struct semsg),0);
    // printf("retnum=%d\n",retnum);
    // printf("测试注册返回码————————>%d\n",semsg->code);
    // printf("测试注册账号————————>%s\n",clmsg.us_name);
    // printf("测试注册密码————————>%s\n",clmsg.us_pswd);
}
//服务器相应管理员登录
void server_check(int sockfd,struct semsg*semsg,struct clmsg clmsg){
    semsg->code=Sq_check_AD(clmsg);
    send(sockfd,semsg,sizeof(struct semsg),0);
}
//服务器相应管理员添加
void server_install(int sockfd,struct semsg*semsg,struct clmsg clmsg){
    semsg->code=Sq_install_MEM(clmsg);
    send(sockfd,semsg,sizeof(struct semsg),0);
}
//服务器相应管理员修改
void server_change(int sockfd,struct semsg*semsg,struct clmsg clmsg){
    semsg->code=Sq_change_MEM(clmsg);
    send(sockfd,semsg,sizeof(struct semsg),0);
}
//服务器相应管理员删除
void server_del(int sockfd,struct semsg *semsg,struct clmsg clmsg){
    semsg->code=Sq_del_MEM(clmsg);
    send(sockfd,semsg,sizeof(struct semsg),0);
}
//服务器相应管理员查询
void server_show(int sockfd,struct semsg*semsg){
    printf("------------------------\n");
    ssize_t renum;
    int a,b,i,j;
    int k=0;
    char **p;
    semsg->code=Sq_show_MEM(&p,&a,&b);
    for(i=0;i<=a;i++)
    {
        memset(semsg->msg,0,sizeof(semsg->msg));
        for(j=0;j<b;j++)
        {
             printf("%s \n",p[k]);
             strcpy(semsg->msg,strcat(semsg->msg,p[k++]));
        }
        renum=send(sockfd,semsg,sizeof(struct semsg),0);
        printf("renum=%d\n",renum);
        printf("%-11s\n",semsg->msg);
    }
    //sqlite3_free(p);
    semsg->code=3;
    send(sockfd,semsg,sizeof(struct semsg),0);
}
